package com.nxy.cpfsfe.busi.unfyag;

import com.adtec.starring.datapool.EPOper;
import com.adtec.starring.exception.BaseException;
import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import com.adtec.starring.struct.dta.DtaInfo;
import com.adtec.starring.util.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.nxy.cpfsfe.PubBeanSerial;
import com.nxy.cpfsfe.enumtype.*;
import com.nxy.cpfsfe.mybatis.mapper.TsPCmdMapper;
import com.nxy.cpfsfe.mybatis.pojo.TbA;
import com.nxy.cpfsfe.mybatis.pojo.TsPCmd;
import com.nxy.cpfsfe.service.CpfsService;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by nxygyzd on 2019/12/7.
 *
 * CPFS.102.001.01-实时贷记异步服务
 */
public class RealCrASrvBean implements CpfsService {

    private final static Logger LOGGER = LoggerFactory.getLogger(RealCrASrvBean.class);

    private SqlSession sqlSession = (SqlSession) SpringUtil.getBean("mybatisSqlSession");

    /**
     * 服务入口
     */
    @Override
    public void doService() {

        Map<String,String> bizMap = null;

        try{
            String tpId = DtaInfo.getInstance().getTpId();
            String nginxUrl = (String) EPOper.get(tpId, "ReqBean[0].head[0].iPAddr");
            String bizMsg = (String) EPOper.get(tpId, "ReqBean[0].body[0].BizMsg");
            if(StringUtils.isBlank(nginxUrl)){
                throw new BaseException(RespCd.NCPO0110,"报文头缺少iPAddr属性");
            }
            if(bizMsg == null){
                throw new BaseException(RespCd.NCPO0110,"报文体缺少BizMsg属性");
            }
            LOGGER.info("接收到[{}]-[{}]：[{}]", MsgCdOrTpEnum.MSGTD_102.getCode(),MsgCdOrTpEnum.MSGTD_102.getName(),bizMsg);

            try{
                bizMap = XmlUtil.xml2Map(bizMsg);
                bizMap.put("NginxUrl",nginxUrl);
            }catch(Exception e){
                throw new BaseException(RespCd.NCPO0101,e,"报文体BizMsg属性值解析失败");
            }

            TsPCmd tsPCmd = insertTsPCmd(bizMap);
            snd321(bizMap,tsPCmd);
        }catch (BaseException e){
            String errMsg = String.format("[%s]报文处理失败，业务交易流水号TrxId=[%s]，[%s]",MsgCdOrTpEnum.MSGTD_102.getCode(),bizMap != null ? bizMap.get("/root/MsgHeader/TrxId") : "",e.getErrorDesc());
            LOGGER.error(errMsg,e);
            sndFailResp(bizMap,e);
        } catch (Throwable e){
            String errMsg = String.format("[%s]报文处理失败，业务交易流水号TrxId=[%s]，[%s]",MsgCdOrTpEnum.MSGTD_102.getCode(),bizMap != null ? bizMap.get("/root/MsgHeader/TrxId") : "",e.getMessage());
            BaseException be = new BaseException(RespCd.NCPS7001,e,errMsg);
            LOGGER.error(be.getErrorDesc(),be);
            sndFailResp(bizMap,be);
        }

    }

    /**
     *  入支付流水表
     */
    private TsPCmd insertTsPCmd(Map<String,String> bizMap){

        try{
            TsPCmdMapper tsPCmdMapper = sqlSession.getMapper(TsPCmdMapper.class);

            TsPCmd tsPCmdIC = new TsPCmd();
            tsPCmdIC.setCpfsD(ToPChlUtil.getInstance().getToPChlByPcId(SysEcodeEnum.NCS.getCode()).getpCD());
            tsPCmdIC.setCpfsSeqNo(PubBeanSerial.getInstance().getSeqNoAndIncrea(TcSequenceEnum.CPFSSEQ.getCode()));
            tsPCmdIC.setBizSysD(DateUtil.str2Date(bizMap.get("/root/MsgHeader/SndDt"), "yyyy-MM-dd HH:mm:ss"));
            tsPCmdIC.setMsgTp(MsgCdOrTpEnum.MSGTD_102.getCode());
            tsPCmdIC.setComId(StringUtil.trimObject(bizMap.get("/root/MsgHeader/MsgTp")));
            tsPCmdIC.setComMsgId(StringUtil.trimObject(bizMap.get("/root/MsgHeader/CorrId")));
            tsPCmdIC.setBizSysId(StringUtil.trimObject(bizMap.get("/root/MsgHeader/SndSysId")));
            tsPCmdIC.setXSeqNo(StringUtil.trimObject(bizMap.get("/root/MsgHeader/TrxId")));
            tsPCmdIC.setBizSta(StatEnum.BIZSTA_ING.getCode());
            tsPCmdIC.setBizTyp(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxCtgy")));
            tsPCmdIC.setBizCls(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/BizKind")));
            tsPCmdIC.setEndToEndId(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/EndToEndId")));
            tsPCmdIC.setShardingId(StringUtil.trimObject(bizMap.get("/root/MsgHeader/SndBankId")));
            tsPCmdIC.setAcqOrgId(StringUtil.trimObject(bizMap.get("/root/MsgHeader/SndBankId")));
            tsPCmdIC.setSndBNo(StringUtil.trimObject(bizMap.get("/root/MsgHeader/SndBankId")));
            tsPCmdIC.setRcvBNo(StringUtil.trimObject(bizMap.get("/root/MsgHeader/RcvBankId")));
            tsPCmdIC.setOrdAmt(new BigDecimal(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxAmt"))));
            if(StringUtils.isNotBlank(bizMap.get("/root/MsgBody/TrxInf/TrxFeeAmt"))){
                tsPCmdIC.setFeAmt(new BigDecimal(bizMap.get("/root/MsgBody/TrxInf/TrxFeeAmt")));
                if(tsPCmdIC.getFeAmt().compareTo(new BigDecimal("0")) == 1){
                    tsPCmdIC.setFeFlg("1");
                }else{
                    tsPCmdIC.setFeFlg("0");
                }
            }else{
                tsPCmdIC.setFeFlg("0");
                tsPCmdIC.setFeAmt(new BigDecimal("0.00"));
            }
//            tsPCmdIC.setCashRmFlg(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxCashTp")));
            tsPCmdIC.setCurTyp(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxCurTp")));
            tsPCmdIC.setpTms(Integer.parseInt(StatEnum.PAY_TIMES_ONE.getCode()));
            tsPCmdIC.setpInfo(StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxPurPose")));
            Map<String ,Object> addInfo = new HashMap<String,Object>();
            addInfo.put("ClbckUrl",StringUtil.trimObject(bizMap.get("/root/MsgHeader/ClbckUrl")));
            addInfo.put("TrxUstrd",StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxUstrd")));
            addInfo.put("NginxUrl",StringUtil.trimObject(bizMap.get("NginxUrl")));
            addInfo.put("RcvSysId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/RcvSysId")));
            addInfo.put("PyrBNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerMmbId")));
            addInfo.put("PyrBrchNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerBrnchId")));
            addInfo.put("PyrA",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerAcctNo")));
            addInfo.put("PyrNm",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerAcctNm")));
            addInfo.put("PyeBNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeMmbId")));
            addInfo.put("PyeBrchNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeBrnchId")));
            addInfo.put("PyeA",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeAcctNo")));
            addInfo.put("PyeNm",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeAcctNm")));
            tsPCmdIC.setAddInfo(new JSONObject(addInfo).toJSONString());
            Map<String ,Object> pInfo = new HashMap<String,Object>();
            pInfo.put("PsBkBlcAmt",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PsBkBlcAmt")));
            pInfo.put("PsBkNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PsBkNo")));
            pInfo.put("InstgId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/InstgId")));
            pInfo.put("InstgAcctId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/InstgAcctId")));
            pInfo.put("InstgAcctIssrId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/InstgAcctIssrId")));
            pInfo.put("TrxDevcInf",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/TrxDevcInf")));
            pInfo.put("BatchId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/BatchId")));
            pInfo.put("SgnNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/SgnNo")));
            pInfo.put("PayerAcctIssrId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PayerAcctIssrId")));
            pInfo.put("InstgTrxTrmTp",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/InstgTrxTrmTp")));
            pInfo.put("PayeeAcctIssrId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PayeeAcctIssrId")));
            pInfo.put("OriTrxId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/OriTrxId")));
            pInfo.put("OriBkTrxId",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/OriBkTrxId")));
            pInfo.put("PyerMrchntNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PyerMrchntNo")));
            pInfo.put("PyerMrchntNm",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PyerMrchntNm")));
            pInfo.put("PyerMrchntShrtNm",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/PyerMrchntShrtNm")));
            pInfo.put("OriTrxAmt",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/OriTrxAmt")));
            pInfo.put("InstgTrxTrmNo",StringUtil.trimObject(bizMap.get("/root/MsgBody/RmtInf/InstgTrxTrmNo")));
            tsPCmdIC.setpInfo(new JSONObject(pInfo).toJSONString());
            tsPCmdIC.setChkASta(StatEnum.CHKASTA_NOT_START.getCode());
            tsPCmdIC.setAdjAFlg("0");
            tsPCmdIC.setCreTm(new Timestamp(System.currentTimeMillis()));
//            tsPCmdIC.setUpdTm(new Timestamp(System.currentTimeMillis()));

            int result = tsPCmdMapper.insertByPo(tsPCmdIC);
            if(result != 1){
                String errMsg = String.format("[%s]入支付流水表[ts_p_cmd]失败，MyCat返回影响行数[%s]",MsgCdOrTpEnum.MSGTD_103.getCode(),result);
                BaseException be = new BaseException(RespCd.NCPS5002,errMsg);
                throw be;
            }

            LOGGER.info("成功录入支付流水表[ts_p_cmd]，前置流水号cpfsSeqNo=[{}]，业务通讯流水号comMsgId=[{}]，业务交易流水号xSeqNo=[{}]",
                    tsPCmdIC.getCpfsSeqNo(),tsPCmdIC.getComMsgId(),tsPCmdIC.getXSeqNo());
            return tsPCmdIC;
        }catch (BaseException e){
            throw e;
        }catch (Exception e){
            String errMsg = String.format("[%s]入支付流水表[ts_p_cmd]失败，[%s]",MsgCdOrTpEnum.MSGTD_103.getCode(),e.getMessage());
            BaseException be = new BaseException(RespCd.NCPS5002,e,errMsg);
            throw be;
        }

    }


    /**
     *  向NCS发送NPS.321报文
     */
    private void snd321(Map<String,String> bizMap,TsPCmd tsPCmd) throws Exception{

        String tpId = DtaInfo.getInstance().getTpId();
        EPOper.put(tpId, "ReqBean[0].head[0].servEcd", RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());
        EPOper.put(tpId, "ReqBean[0].head[0].sndAppCd", tsPCmd.getBizSysId());
        EPOper.put(tpId, "ReqBean[0].head[0].rcvAppCd",SysEcodeEnum.NCS.getCode());
        EPOper.put(tpId, "ReqBean[0].head[0].reqServCd", MsgCdOrTpEnum.MSGCD_321.getCode());
        EPOper.put(tpId, "ReqBean[0].head[0].initrSeqno",System.currentTimeMillis() + "");
        EPOper.getCompSDO(tpId, "ReqBody").setValue("BizMsg",build321(bizMap,tsPCmd));
        String rspsCd = RpcCommUtil.getInstance().rpcFeToGW(tpId, RpcOutputInfoEnum.OutputName_Pub_Cpfsgw.getCode(),RpcOutputInfoEnum.OutPutSvr_CpfsFwdThrdPrtS.getCode());
        if(!rspsCd.equals(RespCd.NCPI0000)){
            String errMsg = String.format("向GW发送[%s]失败，[%s]",MsgCdOrTpEnum.MSGCD_120.getCode(),(String)EPOper.get(tpId, "RspBean[0].head[0].respsInfoDesc"));
            throw new BaseException(RespCd.NCPS7001,errMsg);
        }

    }

    /**
     *  构建NPS.321报文
     */
    private String build321(Map<String,String> bizMap,TsPCmd tsPCmd) throws Exception{

        XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE, SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "NPS_321.xml");
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndAppCd","MPS");
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndMbrCd",tsPCmd.getSndBNo());
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndDt",DateUtil.date2Str(new Date(), "yyyyMMdd"));
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/sndTm",DateUtil.date2Str(new Date(), "HHmmss"));
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/rcvAppCd",SysEcodeEnum.NPS.getCode());
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/rcvMbrCd",tsPCmd.getSndBNo());
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/seqNb",tsPCmd.getComMsgId());
        xmlReqDoc.setNodeContentTrim("/transaction/header/msg/callTyp","ASYN");
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/GrpHdr/MsgId",tsPCmd.getCpfsSeqNo());
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/GrpHdr/CreDtTm",DateUtil.getISODateTime(new Date()));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/GrpHdr/NbOfTxs","1");
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/GrpHdr/SttlmInf/SttlmMtd","CLRG");
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/PmtId/EndToEndId",tsPCmd.getEndToEndId());
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/PmtId/TxId",tsPCmd.getCpfsSeqNo());
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/PmtTpInf/CtgyPurp/Prtry",tsPCmd.getBizTyp());
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/IntrBkSttlmAmt",tsPCmd.getOrdAmt().toPlainString());
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/ChrgBr","DEBT");
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/InstgAgt/FinInstnId/ClrSysMmbId/MmbId",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerMmbId")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/InstgAgt/BrnchId/Id",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerBrnchId")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/InstdAgt/FinInstnId/ClrSysMmbId/MmbId",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeMmbId")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/InstdAgt/BrnchId/Id",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeBrnchId")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/Dbtr/Nm",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerAcctNm")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/Dbtr/PstlAdr/AdrLine",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerAddr")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrAcct/Id/Othr/Id",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerAcctNo")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrAgt/FinInstnId/ClrSysMmbId/MmbId",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerBrnchId")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrAgt/FinInstnId/Nm",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyerInf/PyerBrnchNm")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/CdtrAgt/FinInstnId/ClrSysMmbId/MmbId",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeBrnchId")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/CdtrAgt/FinInstnId/Nm",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeBrnchNm")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/Cdtr/Nm",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeAcctNm")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/Cdtr/PstlAdr/AdrLine",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeAddr")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/CdtrAcct/Id/Othr/Id",StringUtil.trimObject(bizMap.get("/root/MsgBody/PyeeInf/PyeeAcctNo")));
        xmlReqDoc.setNodeContentTrim("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/Purp/Prtry",tsPCmd.getBizCls());

        String[] ustrds = xmlReqDoc.getNodesContent("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/RmtInf/Ustrd");
        String remark =StringUtil.trimObject(bizMap.get("/root/MsgBody/TrxInf/TrxRemark"));
        ustrds[0] ="/Remark/" + (StringUtils.isNotBlank(remark) ? remark : "0");
        ustrds[1] ="/TranChnlTyp/07";
        ustrds[2] ="/SttlmDt/" + DateUtil.date3Str(tsPCmd.getCpfsD());
        ustrds[6] ="/TransFee/0.00";

        xmlReqDoc.setNodesContent("/transaction/body/FIToFICstmrCdtTrf/CdtTrfTxInf/RmtInf/Ustrd",ustrds);
        xmlReqDoc.setNodeContentTrim("/transaction/signature", "");
        String msg = xmlReqDoc.toString();
        msg = msg.replace("<transaction>", "<transaction xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:iso:std:iso:20022:tech:xsd:NPS.120.001.01\">");
        return msg;

    }

    /**
     *  发送失败响应报文
     */
    private void sndFailResp(Map<String,String> bizMap,BaseException e){

        if(bizMap == null){
            return;
        }

        try{
            String resp501Msg = buildNginxResp(bizMap,e,MsgCdOrTpEnum.MSGTD_501.getCode());
            String resp502Msg = buildNginxResp(bizMap,e,MsgCdOrTpEnum.MSGTD_502.getCode());
            HttpUtil.responseSendJson(resp501Msg,resp502Msg,bizMap);
        }catch (BaseException e1){
            String errMsg = String.format("向业务系统[%s]返回[%s]的处理失败响应[CPFS.501或CPFS.502]失败，业务交易流水号TrxId=[%s]，[%s]",bizMap.get("/root/MsgHeader/SndSysId"),MsgCdOrTpEnum.MSGTD_103.getCode(),bizMap.get("/root/MsgHeader/TrxId"),e1.getErrorDesc());
            LOGGER.error(errMsg,e1);
        }catch (Throwable e1){
            String errMsg = String.format("向业务系统[%s]返回[%s]的处理失败响应[CPFS.501或CPFS.502]失败，业务交易流水号TrxId=[%s]，[%s]",bizMap.get("/root/MsgHeader/SndSysId"),MsgCdOrTpEnum.MSGTD_103.getCode(),bizMap.get("/root/MsgHeader/TrxId"),e1.getMessage());
            LOGGER.error(errMsg,e1);
        }

    }

    /**
     * 构建响应Nginx的报文
     */
    private String buildNginxResp(Map<String,String> bizMap,BaseException e,String msgTp) throws Exception{

        Map<String,String> buildMap = new HashMap<String,String>();
        buildMap.put("RcvSysId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/SndSysId")));
        buildMap.put("SndBankId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/RcvBankId")));
        buildMap.put("RcvBankId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/SndBankId")));
        buildMap.put("CorrId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/CorrId")));
        buildMap.put("TrxId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/TrxId")));
        buildMap.put("OriTrxId",StringUtil.trimObject(bizMap.get("/root/MsgHeader/TrxId")));
        buildMap.put("TrxStsCd",e.getErrorCode());
        buildMap.put("BizStsCd",StatEnum.BIZSTA_FAIL.getCode());
        buildMap.put("BizStsDesc",StatEnum.BIZSTA_FAIL.getName());

        if(MsgCdOrTpEnum.MSGTD_501.getCode().equals(msgTp)){
            return ResponseUtil.buildGeneral501Xml(buildMap);
        }else{
            return ResponseUtil.buildGeneral502Xml(buildMap);
        }

    }

}
